package com.yc.cloud.portal.controller;

import cn.hutool.core.util.StrUtil;
import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.basic.constants.TipsConstants;
import com.yc.cloud.common.oauth.wechat.dto.request.WxAuthorizeParam;
import com.yc.cloud.portal.request.MemberAccountLoginParam;
import com.yc.cloud.portal.request.MemberAccountRegisterParam;
import com.yc.cloud.portal.service.MemberAccountService;
import com.yc.cloud.portal.vo.MemberAccountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping(value = "auth")
@Api(value = "权限认证", tags = "权限认证")
public class AuthController {


    private final MemberAccountService memberAccountService;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    public AuthController(MemberAccountService memberAccountService) {
        this.memberAccountService = memberAccountService;
    }


    @ApiOperation(value = "登录", notes = "登录成功后返回Token,最终客户端需在Header中增加Authorization参数名,值为登录返回值")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseResult<String> login(@Validated @RequestBody MemberAccountLoginParam param) {
        String token = memberAccountService.login(param.getUsername(), param.getPassword());
        if (StrUtil.isEmpty(token)) {
            return ResponseResult.failed(TipsConstants.USERNAME_NOT_MATCH_PASSWORD);
        }
        val result = tokenHead + token;
        return ResponseResult.success(result);
    }


    @ApiOperation(value = "微信授权登录", notes = "目前只支持微信授权,如果用户未注册,则一键注册并登录")
    @PostMapping(value = "/authorize")
    public ResponseResult<String> authorize(@Validated @RequestBody WxAuthorizeParam param) {
        String token = memberAccountService.registerOrLoginWithWeChat(param);
        if (StrUtil.isEmpty(token)) {
            return ResponseResult.failed(TipsConstants.USERNAME_NOT_MATCH_PASSWORD);
        }
        val result = tokenHead + token;
        return ResponseResult.success(result);
    }

    @ApiOperation(value = "注册会员")
    @PostMapping(value = "/register")
    @ResponseBody
    public ResponseResult<MemberAccountVo> register(@Validated @RequestBody MemberAccountRegisterParam param) {
        val memberAccount = memberAccountService.register(param);
        val result = memberAccountService.convertVo(memberAccount);
        return result != null ? ResponseResult.success(result) : ResponseResult.failed(TipsConstants.REGISTER_FAILED);
    }


//    @ApiOperation(value = "微信分享")
//    @PostMapping(value = "/wx/share")
//    public ResponseResult<Map<String, String>> getShareInfo(@RequestBody WxParam param) {
//        var accessToken = umsAdminCacheService.getWXAccessToken();
//        if (StrUtil.isEmpty(accessToken)) {
//            accessToken = OfficialAccountUtils.getAccessToken();
//            umsAdminCacheService.setWXAccessToken(accessToken);
//        }
//        if (StrUtil.isEmpty(accessToken)) {
//            return ResponseResult.failed("获取AccessToken失败");
//        }
//        var ticket = umsAdminCacheService.getWXTicket();
//        if (StrUtil.isEmpty(ticket)) {
//            ticket = OfficialAccountUtils.getTicket(accessToken);
//            umsAdminCacheService.setWXTicket(ticket);
//        }
//        if (StrUtil.isEmpty(ticket)) {
//            return ResponseResult.failed("获取Ticket失败");
//        }
//        val result = OfficialAccountUtils.sign(ticket, param.getUrl());
//        return ResponseResult.success(result);
//    }
}
